﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using System.Data;
using Portal.Xml.Utility;
using Portal.Workflow.Render.Template;
using Portal.Workflow.Model;
using Portal.Workflow.Bussiness;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class DefaultHomeWorkflowRender : HomeWorkflowRenderTemplate
    {
        public override string ServiceId
        {
            get
            {
                return "DefaultHomeWorkflowRender";
            }
        }

        public override string ServiceName
        {
            get
            {
                return "Danh sách công việc cần giải quyết";
            }
        }

        class TabItemCls
        {
            public string Alias;
            public string Subject;
            public TabItemCls()
            {
            }
            public TabItemCls(string _Alias, string _Subject)
            {
                Alias = _Alias;
                Subject = _Subject;
            }
        }

        public override AjaxOut Draw(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            TabItemCls[] TabItems = new TabItemCls[]{
                new TabItemCls("workflowaction","Văn bản cần duyệt"),
                //new TabItemCls("workflowaction","Việc cần giải quyết"),
                //new TabItemCls("workflowsent","Việc đã gửi"),
                //new TabItemCls("workflowreceive","Việc đã nhận")
            };

            DateTime dFromDate = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
            DateTime dToDate = System.DateTime.Now;
            string Action = ORenderInfo.Alias;
            if (string.IsNullOrEmpty(Action)) Action = "workflowaction";

            //buil tab
            OAjaxOut.HtmlContent =
                "<div class=tab>\r\n" +
                "   <ul>\r\n";
            for (int iIndex = 0; iIndex < TabItems.Length; iIndex++)
            {
                if (TabItems[iIndex].Alias.ToLower().Equals(Action.ToLower()))
                {
                    OAjaxOut.HtmlContent += "    <li class=active>" + TabItems[iIndex].Subject + "</li>\r\n";
                }
                else
                {
                    OAjaxOut.HtmlContent += "    <li><a href=\"home.aspx?action=" + TabItems[iIndex].Alias + "\">" + TabItems[iIndex].Subject + "</a></li>\r\n";
                }
            }
            OAjaxOut.HtmlContent += "</ul>\r\n" +
                                    "</div>\r\n" +
                                    "<div class=CssTabContent>\r\n";
            if (Action.Equals("workflowaction"))
            {
                OAjaxOut.HtmlContent += ReadingWorkflowAction(SiteId).HtmlContent;
            }
            //if (Action.Equals("workflowsent"))
            //{
            //    OAjaxOut.HtmlContent +=
            //        "<script language=javascript>\r\n" +
            //        "   function ClientFilterSent()\r\n" +
            //        "   {\r\n" +
            //        "       SiteId='" + SiteId + "';\r\n" +
            //        "       fromDate=document.getElementById('txtFromDate').value;\r\n" +
            //        "       toDate=document.getElementById('txtToDate').value;\r\n" +
            //        "       AjaxOut = Portal.Admin.Screen.Render.HomeRenderBll.ReadingWorkflowSent(SiteId,fromDate,toDate).value;\r\n" +
            //        "       document.getElementById('divWorkflowSentContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
            //        "   }\r\n" +
            //        "</script>\r\n" +
            //        "<div id=divWorkflowSentContent>\r\n" +
            //            ReadingWorkflowSent(SiteId, dFromDate.ToString("dd/MM/yyyy"), dToDate.ToString("dd/MM/yyyy")).HtmlContent +
            //        "</div>\r\n";
            //}
            //if (Action.Equals("workflowreceive"))
            //{
            //    OAjaxOut.HtmlContent +=
            //        "<script language=javascript>\r\n" +
            //        "   function ClientFilterReceive()\r\n" +
            //        "   {\r\n" +
            //        "       SiteId='" + SiteId + "';\r\n" +
            //        "       SiteId='" + SiteId + "';\r\n" +
            //        "       fromDate=document.getElementById('txtFromDate').value;\r\n" +
            //        "       toDate=document.getElementById('txtToDate').value;\r\n" +
            //        "       AjaxOut = Portal.Admin.Screen.Render.HomeRenderBll.ReadingWorkflowReceived(SiteId,fromDate,toDate).value;\r\n" +
            //        "       document.getElementById('divWorkflowReceivedContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
            //        "   }\r\n" +
            //        "</script>\r\n" +
            //        "<div id=\"divWorkflowReceivedContent\">\r\n" +
            //            ReadingWorkflowReceived(SiteId, dFromDate.ToString("dd/MM/yyyy"), dToDate.ToString("dd/MM/yyyy")).HtmlContent +
            //        "</div>\r\n";
            //}
            OAjaxOut.HtmlContent += "</div>\r\n";//tabcontent
            return OAjaxOut;
        }


        static AjaxOut ReadingWorkflowAction(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                WorkflowTaskItemCls[] WorkflowTaskItems = WorkflowUtilities.ReadingMyTask(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId, WebSessionUtility.GetCurrentLoginUser().RoleId);
                string Html =
                    "<div class=CssTaskItems>\r\n" ;
                    //UPDATE:BỎ theo y.c
                    //"<h3>Danh sách việc cần thực hiện</h3>\r\n";
                if (WorkflowTaskItems.Length > 0)
                {
                    Html +=
                              "<table>\r\n" +
                              "     <tr>\r\n" +
                              "         <th class=thIndex>#</th>\r\n" +
                              "         <th>Đến từ</th>\r\n" +
                              "         <th>Nội dung</th>\r\n" +
                              "         <th>Thời gian</th>\r\n" +
                              "         <th></th>\r\n" +
                              "         <th></th>\r\n" +
                              "     </tr>\r\n";
                    SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);

                    for (int iIndex = 0; iIndex < WorkflowTaskItems.Length; iIndex++)
                    {
                        int IsReply = WorkflowTaskItems[iIndex].IsReply;
                        string altText = "Yêu cầu mới đến";
                        if (IsReply == 1)
                        {
                            altText = "Yêu cầu bị trả lại";
                        }
                        string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam) + "\\Workflow" + IsReply + ".png";
                        string ProcessButton = "Chưa xử lý";
                        if (WorkflowTaskItems[iIndex].ProcessType.ToLower().Equals("view"))
                        {
                            ProcessButton = "Đã xử lý";
                        }
                        string CurrentProcessBy = "";
                        if (!string.IsNullOrEmpty(WorkflowTaskItems[iIndex].CurrentProcessUser))
                        {
                            CurrentProcessBy = "<div class=CssCurrentProcessBy>Người xử lý: " + WorkflowTaskItems[iIndex].CurrentProcessFullName + "</div>\r\n";
                        }

                        Html += " <tr>\r\n" +
                              "     <td class=tdIndex><img alt=\"" + altText + "\" title=\"" + altText + "\" class=CssWorkflowIcon src=\"" + ButtonUrl + "\"></td>\r\n" +
                              "     <td style=\"width:120px;\">" + WorkflowTaskItems[iIndex].ComeFromFullName + "</td>\r\n" +
                              "     <td>" + WorkflowTaskItems[iIndex].Subject + CurrentProcessBy + "<div class=CssRecordNote>" + WorkflowTaskItems[iIndex].RecordNote + "</div></td>\r\n" +
                              "     <td style=\"width:120px;\">" + WorkflowTaskItems[iIndex].ActionDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
                              "     <td class=tdButton><input type=button class=button value=\"Xem\" onclick=\"javascript:PreviewWorkflow('" + WorkflowTaskItems[iIndex].WorkflowDefId + "','" + WorkflowTaskItems[iIndex].WorkflowActionId + "','" + WorkflowTaskItems[iIndex].RecordId + "');\"></td>\r\n" +
                              "     <td class=tdButton>" + ProcessButton + "</td>\r\n" +
                              " </tr>\r\n";
                    }
                    Html +=
                                  "</table>\r\n";
                }
                else
                {
                    //UPDATE: theo y/c
                    Html += "<div class=CssNoData>Không có văn bản cần duyệt</div>\r\n";
                    //Html += "<div class=CssNoData>Không có công việc nào cần xử lý</div>\r\n";
                }
                Html +=
                              "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ReadingWorkflowSent(string SiteId, string fromDate, string toDate)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        DateTime dFromDate = FunctionUtilities.VNDateToDate(fromDate);
        //        DateTime dToDate = FunctionUtilities.VNDateToDate(toDate);
        //        //new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
        //        //DateTime toDate = System.DateTime.Now;

        //        WorkflowHasSentCls[] WorkflowHasSents = WorkflowUtilities.ReadingWorkflowHasSent(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId, WebSessionUtility.GetCurrentLoginUser().RoleId, dFromDate, dToDate);
        //        string Html =
        //            "<div class=CssTaskItems>\r\n" +
        //            "<h3>Danh sách việc đã xử lý</h3>\r\n" +
        //            "<div class=CssFilter>Từ ngày " + LookUpUtilities.GenerateLookUpDate("txtFromDate", dFromDate) + " đến ngày " + LookUpUtilities.GenerateLookUpDate("txtToDate", dToDate) + " <input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ClientFilterSent();\"></div>\r\n";
        //        if (WorkflowHasSents.Length > 0)
        //        {
        //            Html +=
        //                      "<table>\r\n" +
        //                      "     <tr>\r\n" +
        //                      "         <th class=thIndex>#</th>\r\n" +
        //                //"         <th>Quy trình</th>\r\n" +
        //                      "         <th>Tiêu đề</th>\r\n" +
        //                      "         <th>Nội dung</th>\r\n" +
        //                      "         <th>Thời gian</th>\r\n" +
        //                //"         <th></th>\r\n" +
        //                //"         <th></th>\r\n" +
        //                      "     </tr>\r\n";
        //            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);

        //            for (int iIndex = 0; iIndex < WorkflowHasSents.Length; iIndex++)
        //            {
        //                int IsReply = 0;
        //                string altText = "";
        //                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam) + "\\Workflow" + IsReply + ".png";

        //                Html += " <tr>\r\n" +
        //                      "     <td class=tdIndex><img alt=\"" + altText + "\" title=\"" + altText + "\" class=CssWorkflowIcon src=\"" + ButtonUrl + "\"></td>\r\n" +
        //                    //"     <td style=\"width:120px;\">" + WorkflowHasSents[iIndex].WorkflowDefName + "</td>\r\n" +
        //                      "     <td ><span class=CssItemSubject>" + WorkflowHasSents[iIndex].Subject + "</span><br></td>\r\n" +
        //                      "     <td><span class=CssItemDesc>" + WorkflowHasSents[iIndex].Description + " <div>(" + WorkflowHasSents[iIndex].WorkflowDefName + ")</div></td>\r\n" +
        //                      "     <td style=\"width:120px;\">" + WorkflowHasSents[iIndex].SentDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
        //                    //"     <td class=tdButton><input type=button class=button value=\"Xem\" onclick=\"javascript:PreviewWorkflow('" + WorkflowTaskItems[iIndex].WorkflowDefId + "','" + WorkflowTaskItems[iIndex].WorkflowActionId + "','" + WorkflowTaskItems[iIndex].RecordId + "');\"></td>\r\n" +
        //                    //"     <td class=tdButton>" + ProcessButton + "</td>\r\n" +
        //                      " </tr>\r\n";
        //            }
        //            Html +=
        //                          "</table>\r\n";
        //        }
        //        else
        //        {
        //            Html += "<div class=CssNoData>Không có lịch sử công việc nào đã xử lý</div>\r\n";
        //        }
        //        Html +=
        //                      "</div>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ReadingWorkflowReceived(string SiteId, string fromDate, string toDate)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        DateTime dFromDate = FunctionUtilities.VNDateToDate(fromDate);
        //        DateTime dToDate = FunctionUtilities.VNDateToDate(toDate);

        //        WorkflowHasReceivedCls[] WorkflowHasReceiveds = WorkflowUtilities.ReadingWorkflowHasReceived(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId, WebSessionUtility.GetCurrentLoginUser().RoleId, dFromDate, dToDate);
        //        //DateTime fromDate = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
        //        //DateTime toDate = System.DateTime.Now;

        //        string Html =
        //            "<div class=CssTaskItems>\r\n" +
        //            "<h3>Danh sách việc đã nhận</h3>\r\n" +
        //            "<div class=CssFilter>Từ ngày " + LookUpUtilities.GenerateLookUpDate("txtFromDate", fromDate) + " đến ngày " + LookUpUtilities.GenerateLookUpDate("txtToDate", toDate) + " <input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ClientFilterReceive();\"></div>\r\n";
        //        if (WorkflowHasReceiveds.Length > 0)
        //        {
        //            Html +=
        //                      "<table>\r\n" +
        //                      "     <tr>\r\n" +
        //                      "         <th class=thIndex>#</th>\r\n" +
        //                      "         <th>Người gửi</th>\r\n" +
        //                      "         <th>Tiêu đề</th>\r\n" +
        //                      "         <th>Nội dung</th>\r\n" +
        //                      "         <th>Thời gian</th>\r\n" +
        //                //"         <th></th>\r\n" +
        //                //"         <th></th>\r\n" +
        //                      "     </tr>\r\n";
        //            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);

        //            for (int iIndex = 0; iIndex < WorkflowHasReceiveds.Length; iIndex++)
        //            {
        //                int IsReply = 0;
        //                string altText = "";
        //                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam) + "\\Workflow" + IsReply + ".png";
        //                if (WorkflowHasReceiveds[iIndex].ProcessedDate != new System.DateTime())
        //                {
        //                    ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam) + "\\done.png";
        //                    altText = "Đã xử lý";
        //                }
        //                Html += " <tr>\r\n" +
        //                      "     <td class=tdIndex><img alt=\"" + altText + "\" title=\"" + altText + "\" class=CssWorkflowIcon src=\"" + ButtonUrl + "\"></td>\r\n" +
        //                      "     <td style=\"width:80px;\">" + WorkflowHasReceiveds[iIndex].SendFromUser + "</td>\r\n" +
        //                      "     <td ><span class=CssItemSubject>" + WorkflowHasReceiveds[iIndex].Subject + "</span><br></td>\r\n" +
        //                      "     <td><span class=CssItemDesc>" + WorkflowHasReceiveds[iIndex].Description + " <div>(" + WorkflowHasReceiveds[iIndex].WorkflowDefName + ")</div></td>\r\n" +
        //                      "     <td style=\"width:120px;\">" + WorkflowHasReceiveds[iIndex].ReceivedDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
        //                    //"     <td class=tdButton><input type=button class=button value=\"Xem\" onclick=\"javascript:PreviewWorkflow('" + WorkflowTaskItems[iIndex].WorkflowDefId + "','" + WorkflowTaskItems[iIndex].WorkflowActionId + "','" + WorkflowTaskItems[iIndex].RecordId + "');\"></td>\r\n" +
        //                    //"     <td class=tdButton>" + ProcessButton + "</td>\r\n" +
        //                      " </tr>\r\n";
        //            }
        //            Html +=
        //                          "</table>\r\n";
        //        }
        //        else
        //        {
        //            Html += "<div class=CssNoData>Không có lịch sử công việc nào đã nhận</div>\r\n";
        //        }
        //        Html +=
        //                      "</div>\r\n";
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}
        
    }
}
